<?php
namespace Admin\Controller;

class AdController extends AdminController
{

    /**
     * 首页轮播
     */
    public function lunbo()
    {
        $map['position'] = array('eq', 'index_banner');
        $list = M('ad')->where($map)->order('create_time desc')->select();
        foreach ($list as &$li) {
            if ($li['type']) {
                $li['type'] = "";
            }
            $li['adimg'] = M('picture')->where('id=' . $li['cover_id'])->getField('path');
        }
        $this->assign('_list', $list);
        $this->meta_title = "首页轮播图";
        $this->display();
    }

    /**
     * 添加轮播
     */
    public function lunbo_add()
    {
        $Ad = D('ad');
        if (IS_POST) {
            $lunbo = array('title' => I('title'), 'sort' => I('sort'), 'position' => I('position'), 'cover_id' => I('cover_id'), 'content' => I('content'), 'create_time' => NOW_TIME, 'update_time' => NOW_TIME, 'type' => I('type'), 'url' => I('url'));
            $id = $Ad->add($lunbo);
            $id > 0 ? $this->success('新增成功', U('lunbo')) : $this->error('新增失败');
        }
        $this->assign('self','/admin/ad/lunbo_add');
        $this->meta_title = "新增轮播";
        $this->display();
    }

    /**
     * 编辑轮播
     */
    public function lunbo_edit()
    {
        $Ad = D('ad');
        $id = I('id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');

        if (IS_POST) {
            $lunbo = array('title' => I('title'), 'sort' => I('sort'), 'position' => I('position'), 'cover_id' => I('cover_id'), 'content' => I('content'), 'update_time' => NOW_TIME, 'type' => I('type'), 'url' => I('url'));
            $status = $Ad->where(array('id' => $id))->save($lunbo);
            $status !== false ? $this->success('修改成功', U('lunbo')) : $this->error('修改失败');
        }
        $check = 'checked';
        if ($info['type']) {
            $check1 = 'checked';
            $check2 = '';
        } else {
            $check1 = 'checked';
            $check2 = 'checked';
        }
        $this->assign('self','/admin/ad/lunbo_edit');
        $this->assign('check1', $check1);
        $this->assign('check2', $check2);
        $this->assign('info', $info);
        $this->meta_title = "编辑轮播图";
        $this->display('lunbo_add', U('lunbo'));
    }

    /**
     * 删除轮播
     */
    public function lunbo_del()
    {
        $id = I('id');
        if (!$id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_ad = M("ad");
        $result = $m_ad->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功');
        $this->error('删除失败');
    }

    /**
     * 走马灯广告
     */
    public function loopad(){
        $list = M('loopad')->order('id desc')->select();

        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 添加走马灯广告
     */
    public function add_loopad(){
        if(IS_POST){
            $data['content'] = I('content');
            $data['uptime'] = time();
            $id=M('loopad')->add($data);
            $id > 0 ? $this->success('新增成功', U('loopad')) : $this->error('新增失败');
        }

        $this->display();
    }

    /**
     * 编辑走马灯广告
     */
    public function edit_loopad(){
        $id = I('id');

        if(IS_POST){
            $data['content'] = I('content');
            $data['uptime'] = time();
            $id=M('loopad')->where('id='.$id)->save($data);
            $id > 0 ? $this->success('编辑成功', U('loopad')) : $this->error('编辑失败');        }
        $info = M('loopad')->where('id='.$id)->find();
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * 删除走马灯广告
     */
    public function del_loopad(){
        $id = I('id');
        $id = M('loopad')->where('id='.$id)->delete();
        $id && $this->success('删除成功');
        $this->error('删除失败');
    }
}
?>